package org.wsff.tools.api.gateway.core;

import lombok.Getter;

import java.io.Serializable;
import java.util.Map;

import com.google.common.collect.Maps;

/**
 * 网关上下文抽象类
 *
 * @author ryan
 * @version Id: AbstractGatewayContext.java, v 0.1 2022-02-08 14:05 ryan Exp $$
 */
@Getter
public abstract class AbstractGatewayContext<Request extends Serializable, Response extends Serializable> implements GatewayContext<Request, Response> {

    private static final long   serialVersionUID = -5602445928260075912L;

    /** The Proxy name. */
    private String              name;

    /** The Request. */
    private Request             request;

    /** The Response. */
    private Response            response;

    /** The start time. 调用时间 */
    protected long              reqTime          = System.currentTimeMillis();

    /** The context ext. */
    private Map<String, Object> ext;

    public AbstractGatewayContext(String name, Request request) {
        this.name = name;
        this.request = request;
        this.ext = Maps.newConcurrentMap();
    }

    /**
     * get proxy name
     *
     * @return get proxy name
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * get proxy request.
     *
     * @return request
     */
    @Override
    public Request getRequest() {
        return request;
    }

    /**
     * get proxy response.
     *
     * @return response
     */
    @Override
    public Response getResponse() {
        return response;
    }

    /**
     * set proxy response.
     *
     * @param response response
     */
    @Override
    public void setResponse(Response response) {
        this.response = response;
    }

    /**
     * 添加扩展信息
     *
     * @param key key
     * @return value
     */
    @Override
    public Object getExt(String key) {
        return ext.get(key);
    }

    /**
     * 添加扩展信息
     *  @param key key
     * @param value value
     */
    @Override
    public void addExt(String key, Object value) {
        ext.put(key, value);
    }

}
